/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.command.RoleSaveCommand;
import vip.dexian.admin.command.RoleUpdateCommand;
import vip.dexian.admin.dto.RoleDTO;
import vip.dexian.admin.enums.DataScopeEnum;
import vip.dexian.admin.enums.OperateTypeEnum;
import vip.dexian.admin.log.AdminLog;
import vip.dexian.admin.searcher.RolePageSearcher;
import vip.dexian.admin.service.DepartmentService;
import vip.dexian.admin.service.RoleService;
import vip.dexian.common.Message;
import vip.dexian.common.query.Page;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 角色管理
 *
 * @author 挺好的 2023年06月02日 10:48:14
 */
@Controller ("adminRoleController")
@RequestMapping ("/admin/role")
public class RoleController extends BaseController {

    /**
     * 角色Service
     */
    @Resource (name = "roleServiceImpl")
    private RoleService roleService;

    /**
     * 部门
     */
    @Resource (name = "departmentServiceImpl")
    private DepartmentService departmentService;

    /**
     * 角色列表页面
     *
     * @param searcher
     *         角色查询器
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return list view
     */
    @GetMapping ("/list")
    @AdminLog (module = "role", description = "查看角色列表", type = OperateTypeEnum.LIST)
    public String page (RolePageSearcher searcher, ModelMap model) {

        // 将searcher添加到model中
        searcher.addAttribute(model);

        Page <RoleDTO> page = this.roleService.findPage(searcher.query());

        model.addAttribute("page", page);

        return "/admin/admin/role/list";
    }

    /**
     * 进入角色保存页面
     *
     * @return save view
     */
    @GetMapping ("/save")
    public String add (ModelMap model) {
        model.addAttribute("dataScopeList", DataScopeEnum.values());
        model.addAttribute("departments", this.departmentService.findTree());
        return "/admin/admin/role/save";
    }

    /**
     * 保存角色
     *
     * @param command
     *         角色保存command
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/save")
    @ResponseBody
    @AdminLog (module = "role", description = "保存角色", type = OperateTypeEnum.SAVE)
    public Message save (RoleSaveCommand command) {

        this.roleService.save(command);

        return Message.success();
    }

    /**
     * 进入编辑页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     * @param id
     *         角色id
     *
     * @return update view
     */
    @GetMapping ("/update")
    public String update (Long id, ModelMap model) {
        RoleDTO roleDTO = this.roleService.find(id);
        model.addAttribute("dataScopeList", DataScopeEnum.values());
        model.addAttribute("content", roleDTO);
        model.addAttribute("departments", this.departmentService.findTree());
        return "/admin/admin/role/update";
    }

    /**
     * 更新角色
     *
     * @param command
     *         角色更新command
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/update")
    @ResponseBody
    @AdminLog (module = "role", description = "更新角色", type = OperateTypeEnum.UPDATE)
    public Message update (RoleUpdateCommand command) {
        this.roleService.update(command);
        return Message.success();
    }

    /**
     * 删除角色
     *
     * @param ids
     *         id列表，客户端传递的参数为： ids=1&ids=2&ids=3
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/delete")
    @ResponseBody
    @AdminLog (module = "role", description = "删除角色", type = OperateTypeEnum.DELETE)
    public Message delete (Long[] ids) {
        this.roleService.delete(ids);
        return Message.success();
    }

    /**
     * 校验唯一标识符是否存在
     *
     * @param identity
     *         标识符
     * @param oldIdentity
     *         原标识符
     *
     * @return 标识符是否存在
     */
    @GetMapping ("/check_identity")
    @ResponseBody
    public boolean checkIdentity (String identity, String oldIdentity) {

        if (StringUtils.equals(identity, oldIdentity)) {
            return true;
        }

        return !this.roleService.isIdentityExists(identity);
    }

}
